﻿<span style='font-size:11px; color:#000000; background:#ffffff; '><pre>
<span style='color:#800000; font-weight:bold; '>protected</span> <span style='color:#800000; font-weight:bold; '>void</span> convertToPdfButton_Click<span style='color:#808030; '>(</span><span style='color:#800000; font-weight:bold; '>object</span> sender<span style='color:#808030; '>,</span> EventArgs e<span style='color:#808030; '>)</span>
<span style='color:#800080; '>{</span>
    <span style='color:#696969; '>// Create a PDF document</span>
    Document pdfDocument <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>new</span> Document<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Set license key received after purchase to use the converter in licensed mode</span>
    <span style='color:#696969; '>// Leave it not set to use the converter in demo mode</span>
    pdfDocument<span style='color:#808030; '>.</span>LicenseKey <span style='color:#808030; '>=</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>4W9+bn19bn5ue2B+bn1/YH98YHd3d3c=</span><span style='color:#800000; '>"</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Create a PDF page where to add the first HTML</span>
    PdfPage firstPdfPage <span style='color:#808030; '>=</span> pdfDocument<span style='color:#808030; '>.</span>AddPage<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#800000; font-weight:bold; '>try</span>
    <span style='color:#800080; '>{</span>
        <span style='color:#696969; '>// The element location in PDF</span>
        <span style='color:#800000; font-weight:bold; '>float</span> xLocation <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>float</span><span style='color:#808030; '>.</span>Parse<span style='color:#808030; '>(</span>xLocationTextBox<span style='color:#808030; '>.</span>Text<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
        <span style='color:#800000; font-weight:bold; '>float</span> yLocation <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>float</span><span style='color:#808030; '>.</span>Parse<span style='color:#808030; '>(</span>yLocationTextBox<span style='color:#808030; '>.</span>Text<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// The URL of the HTML page to convert to PDF</span>
        <span style='color:#800000; font-weight:bold; '>string</span> urlToConvert <span style='color:#808030; '>=</span> urlTextBox<span style='color:#808030; '>.</span>Text<span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Create the HTML to PDF element</span>
        HtmlToPdfElement htmlToPdfElement <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>new</span> HtmlToPdfElement<span style='color:#808030; '>(</span>xLocation<span style='color:#808030; '>,</span> yLocation<span style='color:#808030; '>,</span> urlToConvert<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Optionally set the HTML viewer width</span>
        htmlToPdfElement<span style='color:#808030; '>.</span>HtmlViewerWidth <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>int</span><span style='color:#808030; '>.</span>Parse<span style='color:#808030; '>(</span>htmlViewerWidthTextBox<span style='color:#808030; '>.</span>Text<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Optionally set the HTML viewer height</span>
        <span style='color:#800000; font-weight:bold; '>if</span> <span style='color:#808030; '>(</span>htmlViewerHeightTextBox<span style='color:#808030; '>.</span>Text<span style='color:#808030; '>.</span>Length <span style='color:#808030; '>></span> <span style='color:#008c00; '>0</span><span style='color:#808030; '>)</span>
            htmlToPdfElement<span style='color:#808030; '>.</span>HtmlViewerHeight <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>int</span><span style='color:#808030; '>.</span>Parse<span style='color:#808030; '>(</span>htmlViewerHeightTextBox<span style='color:#808030; '>.</span>Text<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Optionally set the HTML content clipping option to force the HTML content width to be exactly HtmlViewerWidth pixels</span>
        htmlToPdfElement<span style='color:#808030; '>.</span>ClipHtmlView <span style='color:#808030; '>=</span> clipContentCheckBox<span style='color:#808030; '>.</span>Checked<span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Optionally set the destination width in PDF</span>
        <span style='color:#800000; font-weight:bold; '>if</span> <span style='color:#808030; '>(</span>contentWidthTextBox<span style='color:#808030; '>.</span>Text<span style='color:#808030; '>.</span>Length <span style='color:#808030; '>></span> <span style='color:#008c00; '>0</span><span style='color:#808030; '>)</span>
            htmlToPdfElement<span style='color:#808030; '>.</span>Width <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>float</span><span style='color:#808030; '>.</span>Parse<span style='color:#808030; '>(</span>contentWidthTextBox<span style='color:#808030; '>.</span>Text<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Optionally set the destination height in PDF</span>
        <span style='color:#800000; font-weight:bold; '>if</span> <span style='color:#808030; '>(</span>contentHeightTextBox<span style='color:#808030; '>.</span>Text<span style='color:#808030; '>.</span>Length <span style='color:#808030; '>></span> <span style='color:#008c00; '>0</span><span style='color:#808030; '>)</span>
            htmlToPdfElement<span style='color:#808030; '>.</span>Height <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>float</span><span style='color:#808030; '>.</span>Parse<span style='color:#808030; '>(</span>contentHeightTextBox<span style='color:#808030; '>.</span>Text<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Optionally set a delay before conversion to allow asynchonous scripts to finish</span>
        htmlToPdfElement<span style='color:#808030; '>.</span>ConversionDelay <span style='color:#808030; '>=</span> <span style='color:#008c00; '>2</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Add the HTML to PDF element to PDF document</span>
        <span style='color:#696969; '>// The AddElementResult contains the bounds of the HTML to PDF Element in last rendered PDF page</span>
        <span style='color:#696969; '>// such that you can start a new PDF element right under it</span>
        AddElementResult result <span style='color:#808030; '>=</span> firstPdfPage<span style='color:#808030; '>.</span>AddElement<span style='color:#808030; '>(</span>htmlToPdfElement<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Save the PDF document in a memory buffer</span>
        <span style='color:#800000; font-weight:bold; '>byte</span><span style='color:#808030; '>[</span><span style='color:#808030; '>]</span> outPdfBuffer <span style='color:#808030; '>=</span> pdfDocument<span style='color:#808030; '>.</span>Save<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Send the PDF as response to browser</span>

        <span style='color:#696969; '>// Set response content type</span>
        Response<span style='color:#808030; '>.</span>AddHeader<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>Content-Type</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>application/pdf</span><span style='color:#800000; '>"</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Instruct the browser to open the PDF file as an attachment or inline</span>
        Response<span style='color:#808030; '>.</span>AddHeader<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>Content-Disposition</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> String<span style='color:#808030; '>.</span>Format<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>attachment; filename=Add_HTML_to_PDF_Elements_to_PDF.pdf; size={0}</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> outPdfBuffer<span style='color:#808030; '>.</span>Length<span style='color:#808030; '>.</span>ToString<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Write the PDF document buffer to HTTP response</span>
        Response<span style='color:#808030; '>.</span>BinaryWrite<span style='color:#808030; '>(</span>outPdfBuffer<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// End the HTTP response and stop the current page processing</span>
        Response<span style='color:#808030; '>.</span>End<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
    <span style='color:#800080; '>}</span>
    <span style='color:#800000; font-weight:bold; '>finally</span>
    <span style='color:#800080; '>{</span>
        <span style='color:#696969; '>// Close the PDF document</span>
        pdfDocument<span style='color:#808030; '>.</span>Close<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
    <span style='color:#800080; '>}</span>
<span style='color:#800080; '>}</span>
</pre>
</span>